<?php
require('lib/verify.php');
require('lib/has_char.php');
require('lib/is_approved.php');

$_category = "warmath";
$_title = "Item";
require('lib/head.php');

$item_id = $_GET["id"];
$system_id = $system["system_id"];

$sql = "select *, wm_bosses.*, item_cache.item_name from wm_items left join item_cache on item_cache.item_id = wm_items.item_id left join wm_bosses on wm_items.item_boss = wm_bosses.boss_id where wm_items.item_id = $item_id";

$result = $db->query($sql);
$item = $db->fetch_assoc($result);

$sql = "select *, wm_characters.* from wm_wishes left join wm_characters on wm_wishes.wish_character = wm_characters.character_id where wish_item = $item_id and wish_system_id = $system_id order by wish_level desc";
$result = $db->query($sql);

boxTop("Warmath: $_title / " . $item["item_name"]);
?>
<?php
show_item($item, $character, $db, "item-view.php?id=" . $item_id, $system);

?>

Drops from <a href="boss.php?id=<?=$item["boss_id"]?>"><?=$item["boss_name"]?></a>
<br />
<br />
<b>DKP Cost</b><br />
<?php
$sql = "select * from wm_item_values where item_id = '" . $item["item_id"] . "' and item_system_id = $system_id";
$ress = $db->query($sql);
$ival = $db->fetch_assoc($ress);
if($ival["item_value"]==0) {
?>
 <i>Unknown cost; item never seen.</i>
<?php
  }
else {
?>
<?=number_format($ival["item_value"], 2)?> dkp
<?php
    }
?>
<br />
<br />


<b>Effective Bids</b> <br />
<?php
if($system["system_tier"]==4) {
?>
<i>Note: Not all min bids are listed here (as a min bid is assumed if an item isn't marked).<br />  Be aware someone with significantly more dkp may take precedence with a min bid.</i><br />
<?php
}
else {
?>
<i>Note: Not all micro bids are listed here (as a micro bid is assumed if an item isn't marked).<br />  Be aware someone with significantly more dkp may take precedence with a micro bid.</i><br />
<?php
   }
?>
<?php
if($system["system_tier"]==4) {
  $perc[3] = 1;
  $perc[2] = 0.6;
  $perc[1] = 0.2;
$floor_max = 2; //2? used to be 10
}
else {
  $perc[4] = 1;
  $perc[3] = 0.6;
  $perc[2] = 0.2;
  $perc[1] = 0.05;
$floor_max = 0.5; //2? used to be 5
}


$ebids = array();
$bid_result = array();
while($ite = $db->fetch_assoc($result)) {
  $bid = $ite["character_dkp"] * $perc[$ite["wish_level"]];
  //  if($bid < ($floor_max)) {
  if($bid < ($floor_max)) { //*$perc[$ite["wish_level"]])) {
    $bid = $floor_max; //*$perc[$ite["wish_level"]];
     //     $bid = 1;
  }
  // if($bid < 2) {
  //    $bid = 1 + $ite["character_dkp"]*0.01;
  //  }
  $ebids[$ite["character_id"]] = $bid;
  $bid_result[$ite["character_id"]] = $ite;
}
arsort($ebids);

while(list($n, $ite) = each($ebids)) {
  $ite = $bid_result[$n];
if($system["system_tier"]==4) {
  $lvl[3] = "Max";
  $lvl[2] = "Mod";
  $lvl[1] = "Min";
}
else {
  $lvl[4] = "Max";
  $lvl[3] = "Mod";
  $lvl[2] = "Min";
  $lvl[1] = "Micro";
}
?>
<b><?=number_format($ebids[$ite["character_id"]], 2)?></b> dkp <?=$lvl[$ite["wish_level"]]?> by <?cclass($ite["character_class"])?> <a href="character.php?id=<?=$ite["wish_character"]?>"><?=$ite["character_name"]?></a> (<?=number_format($ite["character_dkp"], 2)?> dkp)<br />
<?php
}
?>

<br />
<b>Looted by</b><br />
<?php
$lit = $item["item_id"];
$sql = "select *, wm_characters.*, wm_credits.*, wm_instances.*, wm_raids.*, wm_bosses.*, wm_item_values.* from wm_loots left join wm_characters on wm_characters.character_id = loot_character left join item_cache on wm_loots.loot_item = item_cache.item_id left join wm_credits on wm_credits.credit_id = wm_loots.loot_credit left join wm_bosses on wm_bosses.boss_id = wm_credits.credit_boss left join wm_raids on wm_raids.raid_id = wm_credits.credit_raid left join wm_instances on wm_instances.instance_id = wm_bosses.boss_instance left join wm_item_values on wm_item_values.item_id = wm_loots.loot_item where wm_loots.loot_item = $lit and wm_credits.credit_system_id = $system_id and wm_item_values.item_system_id=$system_id order by wm_credits.credit_date desc";
$result = $db->query($sql);
?>
<i>Looted <?=$db->num_rows($result)?> times.</i><br />
<?php
if($system["system_tier"]==4) {
$wish_text[1] = "Min";
$wish_text[2] = "Mod";
$wish_text[3] = "Max";
}
else {
$wish_text[1] = "Micro";
$wish_text[2] = "Min";
$wish_text[3] = "Mod";
$wish_text[4] = "Max";
}


while($loot = $db->fetch_assoc($result)) {

?>
<a href="character.php?id=<?=$loot["loot_character"]?>"><?=$loot["character_name"]?></a> <?=$wish_text[$loot["loot_level"]]?> (<?=number_format($loot["loot_bid"], 2)?> bid) (<?=number_format($loot["item_value"], 2)?> paid)<br />
<a href="credit.php?id=<?=$loot["credit_id"]?>"><?=$loot["boss_name"]?>, <?=date("g:i a", strtotime($loot["credit_date"]))?></a><br />
<a href="raid.php?id=<?=$loot["raid_id"]?>"><?=$loot["instance_name"]?>, <?=date("D d M Y", strtotime($loot["raid_date"]))?></a><br/>
<br />
<?php
}
?>
<?php
boxBot();
?>

<?php
require('lib/foot.php');
?>

